package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ChooseSubjectData;
import com.eduhzy.ttw.commonsdk.entity.ClassTeacherData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerChooseSubjectComponent;
import com.eduhzy.ttw.teacher.mvp.contract.ChooseSubjectContract;
import com.eduhzy.ttw.teacher.mvp.presenter.ChooseSubjectPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

;

/**
 * 选择任教学科
 *
 * @author xubo
 * @date 2018-7-16 15:42:19
 */
@Route(path = RouterHub.APP_CHOOSESUBJECTACTIVITY)
public class ChooseSubjectActivity extends BaseActivity<ChooseSubjectPresenter> implements ChooseSubjectContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @Autowired(name = Constants.ROUTER_DATA)
    ClassTeacherData classTeacherData;
    @Inject
    BaseQuickAdapter<ChooseSubjectData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    List<ChooseSubjectData> lists;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerChooseSubjectComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_choose_subject;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.getSubject(classTeacherData.getClassId(), classTeacherData.getUserId());
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        swipeLayout.setOnRefreshListener(() -> {
                    mPresenter.getSubject(classTeacherData.getClassId(), classTeacherData.getUserId());
                }
        );
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ChooseSubjectData chooseClassData = lists.get(position);
            chooseClassData.setSelect(!chooseClassData.isSelect());
            mAdapter.notifyDataSetChanged();
        });

    }

    @Override
    public void addFooter() {
        if (mAdapter.getFooterLayoutCount() <= 0) {
            View footer = View.inflate(getActivity(), R.layout.rv_list_footer_button, null);
            mAdapter.addFooterView(footer);
            ((Button) footer.findViewById(R.id.bt_ok)).setOnClickListener(v -> {
                if (ObjectUtils.isNotEmpty(lists)) {
                    StringBuilder sb = new StringBuilder();
                    for (ChooseSubjectData data : lists) {
                        if (data.isSelect()) {
                            sb.append(data.getCode());
                            sb.append(",");
                        }
                    }
                    String ids = "";
                    if (sb.length() > 0) {
                        ids = sb.substring(0, sb.length() - 1);
                    }
                    mPresenter.updateTeaSubject(classTeacherData.getUserId(), classTeacherData.getClassId(), classTeacherData.getUserId(), ids);
                }
            });
        }
    }

    @Override
    public void showLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
